$(document).ready(function() {
	$Base.isLoginManage(getOrderList);
});

function getOrderList() {
	var id = $.trim($('#search_id').val());
	var status = $('#search_status').val()
	var url = '/json?action=MANAGE_QUERY_T_ORDER_PAGER_ACTION&ORDER_ID=' + id + "&STATUS="+status;
	var current = $('#pageId').val();
	$UI.changePage(url,10,'T_ORDER','#list',printRes,'#page','<tr><td colspan="12"><div style="text-align:center">暂无数据</div></td></tr>',current,null,'#pageId');
}

function printRes(obj,result,i,n,total,current){
	var trStr = '';
	trStr+=	'<tr '+ (i%2 == 0 ? "" : "class=\'alt\'") +'>';
	trStr+=	'<td>'+ obj.ORDER_ID +'</td>';
	trStr+=	'<td>'+ obj.CONSIGNEE +'</td>';
	trStr+=	'<td>'+ (obj.MOBILE == '' ? obj.TELEPHONE : obj.MOBILE) +'</td>';
	trStr+=	'<td>'+ $Base.getDeliverTime(obj.DELIVER_TIME) +'</td>';
	trStr+=	'<td>'+ $Base.getPayMethod(obj.PAY_METHOD) +'</td>';
	trStr+=	'<td>'+ $Base.getDeliverMethod(obj.DELIVER_METHOD) +'</td>';
	trStr+=	'<td>'+ obj.PAYMENT +'</td>';
	trStr+=	'<td>'+ obj.ORDER_POINTS +'</td>';
	trStr+=	'<td>'+ $Util.toChar(obj.ORDER_DATE, 'yyyy-mm-dd hh24:mi:ss') +'</td>';
	trStr+=	'<td>',
	trStr+=	'<a href="print_pick.html?id='+obj.ORDER_ID+'" title="配送单" target="_blank">配</a> ';
	trStr+=	'<a href="javascript:void(0)" title="快递单">递</a>';
	trStr+=	'</td>';
	trStr+=	'<td>'+ $Base.getOrderStatus(obj.STATUS) +'</td>';
	trStr+=	'<td>';
	trStr+=	'<a href="javascript:$Base.isLoginManage(detail,\''+obj.ORDER_ID+'\')">详细</a> ';
			switch(obj.STATUS){
				case 3:
					trStr+=	'<a href="javascript:$Base.isLoginManage(update,\''+obj.ORDER_ID+'\')">修改</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(pick,\''+obj.ORDER_ID+'\')">配货</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(cancel,\''+obj.ORDER_ID+'\')">取消</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(exception,\''+obj.ORDER_ID+'\')">异常</a> ';
					break;
				case 4:
					trStr+=	'<a href="javascript:$Base.isLoginManage(outbound,\''+obj.ORDER_ID+'\')">出库</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(cancel,\''+obj.ORDER_ID+'\')">取消</a> ';
					break;
				case 5:
					trStr+=	'<a href="javascript:$Base.isLoginManage(deliver,\''+obj.ORDER_ID+'\')">发货</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(cancel,\''+obj.ORDER_ID+'\')">取消</a> ';
					break;
				case 6:
					trStr+=	'<a href="javascript:$Base.isLoginManage(finish,\''+obj.ORDER_ID+'\')">完成</a> ';
					trStr+=	'<a href="javascript:$Base.isLoginManage(rebated,\''+obj.ORDER_ID+'\')">退款</a> ';
					break;
				case 7:
					trStr+=	'<a href="javascript:$Base.isLoginManage(rebated,\''+obj.ORDER_ID+'\')">退款</a> ';
					break;
			}
	trStr+=	'</td>';
	trStr+=	'</tr>';
	$(result).append(trStr);
}

var msg_second = 2000;
function detail(id){
		$.zxxbox.ajax("detail.html", {s:Math.random()}, {
		title:"订单详细",
		drag:true, 
		protect:false, 
		ajaxCallback: function(){
			$('#ORDER_ID').val(id);
			var url = '/json?action=MANAGE_GET_ORDER_BY_ID_ACTION';
			$.post(url,{ORDER_ID:id},function(data){
				if(data.success){
					var obj = data.root.T_ORDER.rs[0];
					var order = obj.ORDER;
					$('#FORM_ORDER_ID').html(id);
					$('#ORDER_DATE').html($Util.toChar(order.ORDER_DATE, 'yyyy-mm-dd hh24:mi:ss'));
					$('#STATUS').html($Base.getOrderStatus(order.STATUS));
					$('#DELIVER_METHOD').html($Base.getDeliverMethod(order.DELIVER_METHOD));
					$('#PAY_METHOD').html($Base.getPayMethod(order.PAY_METHOD));
					$('#DELIVER_TIME').html($Base.getDeliverTime(order.DELIVER_TIME));
					$('#PAYMENT').html("￥"+order.PAYMENT);
					$('#ORDER_POINTS').html(order.ORDER_POINTS);
					$('#MESSAGE').html(order.MESSAGE);
					$('#CONSIGNEE').html(order.CONSIGNEE);
					$('#MOBILE').html(order.MOBILE);
					$('#ZIP_CODE').html(order.ZIP_CODE);
					$('#TELEPHONE').html(order.TELEPHONE);
					$('#FINAL_ADDRESS').html(order.FINAL_ADDRESS);
					
					var productList = obj.DETAIL;
					var html = '';
					$.each(productList, function(i, o){
						html += '<tr>';
						html += '<td class="tab_td" style="border-left: 1px solid #999999;">' + o.PRODUCT_NO + '</td>';
						html += '<td class="tab_td"><a href="../../product/file.html?id='+o.PRODUCT_ID+'" target="_blank"><img src="../../upload/product/'+o.PRODUCT_NO+'.jpg" width="50" height="50" align="middle" /></a>' +
								'<span style="padding:5px;"><a href="../../product/file.html?id='+o.PRODUCT_ID+'" target="_blank">' + o.PRODUCT_NAME + '</a></span></td>';
						html += '<td class="tab_td">￥' + o.MEMBER_PRICE+ '</td>';
						html += '<td class="tab_td">' + o.QTY+ '</td>';
						html += '<td class="tab_td">' + o.POINT + '</td>';
						html += '<td class="tab_td">￥' + o.AMOUNT + '</td>';
						html += '</tr>';
					});
					$('#plist').html(html);
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	});
}

function update(id){
	$.zxxbox.ajax("update.html", {s:Math.random()}, {
		title:"修改订单",
		drag:true, 
		protect:false, 
		ajaxCallback: function(){
			$('#ORDER_ID').val(id);
			var url = '/json?action=MANAGE_GET_ORDER_BY_ID_ACTION';
			$.post(url,{ORDER_ID:id},function(data){
				if(data.success){
					var obj = data.root.T_ORDER.rs[0];
					var order = obj.ORDER;
					$('#FORM_ORDER_ID').html(id);
					$('#ORDER_DATE').html($Util.toChar(order.ORDER_DATE, 'yyyy-mm-dd hh24:mi:ss'));
					$('#STATUS').html($Base.getOrderStatus(order.STATUS));
					$('#DELIVER_METHOD').val(order.DELIVER_METHOD);
					$('#PAY_METHOD').val(order.PAY_METHOD);
					$('#DELIVER_TIME').val(order.DELIVER_TIME);
					$('#PAYMENT').html("￥"+order.PAYMENT);
					$('#ORDER_POINTS').html(order.ORDER_POINTS);
					$('#MESSAGE').html(order.MESSAGE);
					$('#CONSIGNEE').val(order.CONSIGNEE);
					$('#MOBILE').val(order.MOBILE);
					$('#ZIP_CODE').val(order.ZIP_CODE);
					$('#TELEPHONE').val(order.TELEPHONE);
					$('#FINAL_ADDRESS').val(order.FINAL_ADDRESS);
					
					var productList = obj.DETAIL;
					var html = '';
					$.each(productList, function(i, o){
						html += '<tr>';
						html += '<input type="hidden" name="PRODUCT_ID" value="'+o.PRODUCT_ID+'" />';
						html += '<td class="tab_td" style="border-left: 1px solid #999999;">' + o.PRODUCT_NO + '</td>';
						html += '<td class="tab_td"><a href="../../product/file.html?id='+o.PRODUCT_ID+'" target="_blank"><img src="../../upload/product/'+o.PRODUCT_NO+'.jpg" width="50" height="50" align="middle" /></a>' +
								'<span style="padding:5px;"><a href="../../product/file.html?id='+o.PRODUCT_ID+'" target="_blank">' + o.PRODUCT_NAME + '</a></span></td>';
						html += '<td class="tab_td"><input type="text" id="price'+o.PRODUCT_ID+'" name="MEMBER_PRICE" value="' + o.MEMBER_PRICE+ '" style="width:50px;"/></td>';
						html += '<td class="tab_td"><input type="text" id="qty'+o.PRODUCT_ID+'" name="QTY" value="' + o.QTY+ '" style="width:40px;" readonly/></td>';
						html += '<td class="tab_td">' + o.POINT + '</td>';
						html += '<td class="tab_td">' + o.AMOUNT + '</td>';
						html += '</tr>';
					});
					$('#plist').html(html);
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	});
}

function pick(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_PICK_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					print_order(id);
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json')
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("进入配货流程请先确认订单信息无误后，再点击【确定】进行配货", btnlist, {title: "操作确认"});
}

function print_order(id){
	var btnlist = [{
		id: 'ok',
		name: '打印配送单',
		click: function(){
			window.open('print_pick.html?id='+id);
		}
	},{
		id: 'printed',
		name: '已打印单据',
		click: function(){
			$.zxxbox.hide();
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("订单已转入配货状态，请打印配送单进行配送", btnlist, {title: "操作确认"});
}

function outbound(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_OUTBOUND_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单已转入出库状态，请尽快联系送货人员进行送货。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("进入出库流程请确定订单的商品已配备齐全后，再点击【确定】进入等待发货", btnlist, {title: "操作确认"});
}

function deliver(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_DELIVER_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单已转入发货状态，请等待订单完成。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("请确定已按照配送单上的商品清单配备完毕并以打包，如果已完成请点击【确定】安排配送人员", btnlist, {title: "操作确认"});
}

function finish(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_FINISH_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单交易已完成。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("请根据回执的单据确定订单是否完成，如果已完成请点击【确定】", btnlist, {title: "操作确认"});
}

function cancel(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var result = $.trim($('#RESULT').val());
			if(result.length == 0){
				alert("请填写取消原因");
				return;
			}
			var url = '/json?action=MANAGE_CANCEL_ACTION&ORDER_ID='+id;
			$.post(url,{RESULT : result},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单交易已取消。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	var html = '(原因将显示给客户，请写明具体情况)<br/><textarea cols="50" rows="3" id="RESULT" name="RESULT"></textarea>';
	$.zxxbox.ask(html, btnlist, {title: "请填写取消原因"});
}

function exception(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_EXCEPTION_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单已转入异常状态。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("您确定进行订单编号【" + id + "】的异常处理流程吗？", btnlist, {title: "操作确认"});
}

function rebateing(id){
	var sureCall = function(){
		var url = '/json?action=MANAGE_REBATEING_ACTION&ORDER_ID='+id;
		$.post(url,{},function(data){
			if(data.success){
				getOrderList();
				$.zxxbox.msg("订单已转入退款状态。", msg_second, 'success');
			} else {
				$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
			}
		},'json');
	};
	var options = {
		title: "操作确认"
	};
	$.zxxbox.ask("您确定进行订单编号【" + id + "】的退款流程吗？", sureCall, null, options);
}

function rebated(id){
	var btnlist = [{
		id: 'ok',
		name: '确定',
		click: function(){
			var url = '/json?action=MANAGE_REBATED_ACTION&ORDER_ID='+id;
			$.post(url,{},function(data){
				if(data.success){
					getOrderList();
					$.zxxbox.msg("订单已完成退款。", msg_second, 'success');
				} else {
					$.zxxbox.msg(data.errors.errmsg, msg_second, 'error');
				}
			},'json');
		}
	},{
		id: 'cancel',
		name: '取消',
		click: function(){
			$.zxxbox.hide();
		},
		css: 'cancel_btn'
	}];
	$.zxxbox.ask("您确定进行订单编号【" + id + "】的完成退款流程吗？", btnlist, {title: "操作确认"});
}